/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.univcasa.view.actions;

import com.univcasa.db.model.CompteEtudiant;
import com.univcasa.managing.ComptesStudentsManager;
import com.univcasa.managing.impl.ComptesStudentsManagerImpl;
import com.univcasa.view.forms.ajoutDBCompteStdForm;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.helpers.DateTimeDateFormat;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author issam
 */
public class ajoutDBCompteStdSimul extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ComptesStudentsManager manager = new ComptesStudentsManagerImpl();
        
        ajoutDBCompteStdForm aform = (ajoutDBCompteStdForm) form;
        String apogee = aform.getApogee().replaceAll(" ", "");
        String cne = aform.getCne().replaceAll(" ", "");
        String cin = aform.getCin().replaceAll(" ", "");
        String nom = aform.getNom();
        String prenom = aform.getPrenom();
        String etab = aform.getEtab();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date naissance = dateFormat.parse( aform.getNaissance() );
        String sexe = aform.getSexe();

        CompteEtudiant std = new CompteEtudiant(cne, apogee, nom, prenom, etab, "XXXXX", sexe);
        std.setCin(cin);
        std.setDiplome("XXXX");
        std.setNaissance(naissance);
        std.setPassword( apogee );

        HashMap criteres = new HashMap();
        criteres.put("apogee", apogee);

        if (  manager.isExists(std)){
            ActionErrors actionErrors = new ActionErrors();
            actionErrors.add("error", new ActionMessage("std.exists")) ;
            saveErrors(request, actionErrors);
            return mapping.findForward("failure");
        }
        else{
            manager.ajouterCompteEtudiant(std);
            request.setAttribute("compte", std);
            return mapping.findForward(SUCCESS);
        }
    }
}
